package br.ucb.mercadojogador.HIB;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.exception.ConstraintViolationException;

import br.ucb.mercadojogador.beans.Time;

public class TimeHIB {

	public void salvar(Time time) {
		Session session = HibernateUtil.getSession();
		Transaction transaction = session.beginTransaction();
		session.saveOrUpdate(time);
		transaction.commit();
		session.close();
	}
	
	public void excluir(Time time) throws ConstraintViolationException {
		Session session = HibernateUtil.getSession();
		Transaction transaction = session.beginTransaction();
		session.delete(time);
		transaction.commit();
		session.close();
	}
	
	@SuppressWarnings("unchecked")
	public List<Time> listar() {
	Session session = HibernateUtil.getSession();
		try {
			return session.createCriteria(Time.class).list();
		} finally {
			session.close();
		}
	}
	
	public Time consultar(int idTime) {
	Session session = HibernateUtil.getSession();
		try {
			Time time = (Time) session.get(Time.class,idTime);
			return time;
		} finally {
			session.close();
		}
	}
}
